<template>
  <el-dialog
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
    width="40%"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-width="80px"
      style="width: 100%"
    >
      <el-form-item label="档案编号" prop="ArchivesCode">
        <el-input v-model="temp.ArchivesCode" :disabled="codeDisabled" />
      </el-form-item>
      <el-form-item label="档案名称" prop="ArchivesName">
        <el-input v-model="temp.ArchivesName" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">返回</el-button>
      <el-button
        type="primary"
        @click="dialogStatus === 'create' ? createData() : updateData()"
      >提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  addCustomArchives,
  editCustomArchives
} from '@/api/customarchives/customarchives'
export default {
  data() {
    return {
      codeDisabled: false,
      dialogFormVisible: false, // 新增账户信息弹窗
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },

      // 新增分组表单
      temp: {
        Id: '',
        UnitId: '', // 所属单位ID
        ArchivesCode: '', // 档案编号
        ArchivesName: '' // 档案名称
      },
      // 验证规则
      rules: {
        ArchivesName: [{ required: true, message: '档案名称必填', trigger: 'blur' }],
        ArchivesCode: [{ required: true, message: '档案编码必填', trigger: 'blur' }]
      }
    }
  },

  created() {

  },
  methods: {
    // 初始化
    init() {
      this.temp = {
        Id: '',
        UnitId: '', // 所属单位ID
        ArchivesCode: this.getyymm(), // 档案编号
        ArchivesName: '' // 档案名称
      }
    },
    // 打开新增分组弹窗
    addShow() {
      this.codeDisabled = false
      this.dialogStatus = 'create'
      this.init()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    getyymm() {
    // 创建日期对象
      var date = new Date()
      // 获取年份
      var yy = date.getFullYear()
      // 获取月份
      var mm = date.getMonth() + 1
      // 如果月份小于10 前面加0
      mm = (mm < 10 ? '0' + mm : mm)
      var dd = date.getDate()
      var hh = date.getHours()
      var m = date.getMinutes()
      var s = date.getSeconds()
      // 返回日期
      return (yy.toString() + mm.toString() + dd.toString() + hh.toString() + m.toString() + s.toString())
    },

    // 提交新增分组
    createData() {
      var _this = this
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          addCustomArchives(_this.temp).then(() => {
            _this.dialogFormVisible = false
            _this.init()
            // 回调方法
            this.$emit('success')
          })
        }
      })
    },
    // 打开修改分组弹窗
    updateShow(row) {
      this.codeDisabled = true
      this.init()
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改分组
    updateData() {
      var _this = this
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, _this.temp)
          editCustomArchives(tempData).then(() => {
            _this.dialogFormVisible = false
            _this.init()
            this.$emit('success')
          })
        }
      })
    }
  }
}
</script>
